/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package com.xhh.hybriddemo.ui.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.xhh.hybriddemo.*;
import com.xhh.hybriddemo.R;
import com.xhh.hybriddemo.base.BaseActivity;
import com.xhh.hybriddemo.base.BaseFragment;
import com.xhh.hybriddemo.common.AppContext;
import com.xhh.hybriddemo.ui.fragment.BorrowFragment;
import com.xhh.hybriddemo.ui.fragment.HomeFragment;
import com.xhh.hybriddemo.ui.fragment.ServiceFragment;
import com.xhh.hybriddemo.ui.fragment.UserFragment;

import org.apache.cordova.*;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends FragmentActivity  implements RadioGroup.OnCheckedChangeListener {
    private long mExitTime;
    private int position;
    private BaseFragment mContext;
    protected Toolbar mToolbar;
    private List<BaseFragment> mFragments = new ArrayList<>();
    private TextView mCustomTitleTextView;
    private Unbinder unbinder;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppContext.getInstance().addActivity(this.getClass(), this);
        unbinder = ButterKnife.bind(this);
        init();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppContext.getInstance().removeActivity(this.getClass());
        unbinder.unbind();
    }


    private void init() {
        initFragment();
        initTab();
    }

    private void initFragment() {
        if (mFragments!=null){
            mFragments.clear();
            mFragments.add(new HomeFragment());
            mFragments.add(new BorrowFragment());
            mFragments.add(new ServiceFragment());
            mFragments.add(new UserFragment());
        }
    }

    private void initTab(){
        rgMain.setOnCheckedChangeListener(this);
        rgMain.check(R.id.rb_home);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                position = 0;
                hideSystemUI();
                break;
            case R.id.rb_borrow:
                position = 1;
                showSystemUI();
                break;
            case R.id.rb_service:
                position = 2;
                showSystemUI();
                break;
            case R.id.rb_user:
                position = 3;
                showSystemUI();
                break;
        }
        BaseFragment baseFragment = getFragment(position);
        switchFragment(mContext,baseFragment);
    }

    private BaseFragment getFragment(int position){
        if (mFragments!=null&&mFragments.size()!=0){
            BaseFragment locationFragment = mFragments.get(position);
            return locationFragment;
        }
        return null;
    }

    private void switchFragment(Fragment fromFragment, BaseFragment nextFragment) {
        if (mContext != nextFragment){
            mContext = nextFragment;
            if (nextFragment!=null){

                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                //判断nextFragment是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.add(R.id.frameLayout, nextFragment).commitAllowingStateLoss();
                } else {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commitAllowingStateLoss();
                }
            }
        }
    }

    /**
     * 物理返回键
     */
    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(this,"再按一次退出",Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            exit();
        }
    }

    private void exit() {
        AppContext.getInstance().exit();
    }

    /*
 * 设置沉浸式
 * */
    private void hideSystemUI(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }


    private void showSystemUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_VISIBLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(getResources().getColor(R.color.blue));
        }
    }
}
